/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainFrame.java
 *
 * Created on 2011-7-23, 15:10:37
 */
package edu.qmul.ns2;

import edu.qmul.ns2.agent.AgentLabel;
import edu.qmul.ns2.agent.AgentType;
import edu.qmul.ns2.agent.DrawAgent;
import edu.qmul.ns2.agent.TCPLabel;
import edu.qmul.ns2.agent.UDPLabel;
import edu.qmul.ns2.computer.ComputerLabel;
import edu.qmul.ns2.line.DrawLine;
import edu.qmul.ns2.computer.DrawNode;
import edu.qmul.ns2.computer.NodeLabel;
import edu.qmul.ns2.computer.NodeType;
import edu.qmul.ns2.line.ExtendLineSegmentComponent;
import edu.qmul.ns2.line.LinkType;
import java.awt.Component;
import java.awt.event.ItemEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import me.zhy.swing.AWTUtilities;

/**
 *
 * @author Moon
 */
public class MainFrame extends javax.swing.JFrame {

    String tclName;
    String traceFileName;
    String traceQueueName;
    String namName;

    /** Creates new form MainFrame */
    public MainFrame() {
        tclName = "1";
        traceFileName = "2";
        traceQueueName = "3";
        namName = "4";
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jSplitPane1 = new javax.swing.JSplitPane();
        jSplitPane2 = new javax.swing.JSplitPane();
        jSplitPane3 = new javax.swing.JSplitPane();
        mainPanel = new edu.qmul.ns2.MainPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        chooseButton = new javax.swing.JToggleButton();
        computerButton = new javax.swing.JToggleButton();
        routButton = new javax.swing.JToggleButton();
        switchButton = new javax.swing.JToggleButton();
        jPanel2 = new javax.swing.JPanel();
        jToggleButton3 = new javax.swing.JToggleButton();
        duplexLineButton = new javax.swing.JToggleButton();
        simplexLineButton = new javax.swing.JToggleButton();
        lineButton = new javax.swing.JToggleButton();
        jPanel3 = new javax.swing.JPanel();
        jToggleButton1 = new javax.swing.JToggleButton();
        udpButton = new javax.swing.JToggleButton();
        tcpButton = new javax.swing.JToggleButton();
        nullButton = new javax.swing.JToggleButton();
        jPanel4 = new javax.swing.JPanel();
        jToolBar1 = new javax.swing.JToolBar();
        jButton1 = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        configureButton = new javax.swing.JButton();
        tcl = new javax.swing.JButton();
        stateLabel = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jSplitPane1.setBorder(null);
        jSplitPane1.setResizeWeight(0.2);

        jSplitPane2.setBorder(null);
        jSplitPane2.setResizeWeight(0.75);

        jSplitPane3.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        jSplitPane3.setResizeWeight(0.8);

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 21, Short.MAX_VALUE)
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 239, Short.MAX_VALUE)
        );

        jSplitPane3.setLeftComponent(mainPanel);

        jScrollPane1.setBorder(null);
        jSplitPane3.setRightComponent(jScrollPane1);

        jSplitPane2.setLeftComponent(jSplitPane3);

        jSplitPane1.setRightComponent(jSplitPane2);

        jPanel1.setMinimumSize(new java.awt.Dimension(50, 33));
        jPanel1.setPreferredSize(new java.awt.Dimension(50, 33));

        buttonGroup1.add(chooseButton);
        chooseButton.setSelected(true);
        chooseButton.setText("Choose");

        buttonGroup1.add(computerButton);
        computerButton.setText("Computer");
        computerButton.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                computerButtonItemStateChanged(evt);
            }
        });

        buttonGroup1.add(routButton);
        routButton.setText("Rout");
        routButton.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                routButtonItemStateChanged(evt);
            }
        });

        buttonGroup1.add(switchButton);
        switchButton.setText("Switch");
        switchButton.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                switchButtonItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(computerButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(chooseButton)
                    .addComponent(routButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(switchButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(240, 240, 240))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addComponent(chooseButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(computerButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(routButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(switchButton)
                .addContainerGap(116, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Node", jPanel1);

        buttonGroup1.add(jToggleButton3);
        jToggleButton3.setText("Choose");

        buttonGroup1.add(duplexLineButton);
        duplexLineButton.setText("Duplex");
        duplexLineButton.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                duplexLineButtonItemStateChanged(evt);
            }
        });

        buttonGroup1.add(simplexLineButton);
        simplexLineButton.setText("Simplex");
        simplexLineButton.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                simplexLineButtonItemStateChanged(evt);
            }
        });

        buttonGroup1.add(lineButton);
        lineButton.setText("Line");
        lineButton.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                lineButtonItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(lineButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(simplexLineButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(duplexLineButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jToggleButton3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(241, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addComponent(jToggleButton3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(duplexLineButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(simplexLineButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lineButton)
                .addContainerGap(112, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Link", jPanel2);

        buttonGroup1.add(jToggleButton1);
        jToggleButton1.setText("Choose");

        buttonGroup1.add(udpButton);
        udpButton.setText("UDP");
        udpButton.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                udpButtonItemStateChanged(evt);
            }
        });

        buttonGroup1.add(tcpButton);
        tcpButton.setText("TCP");
        tcpButton.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                tcpButtonItemStateChanged(evt);
            }
        });

        buttonGroup1.add(nullButton);
        nullButton.setText("NULL");
        nullButton.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                nullButtonItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(nullButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(udpButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(tcpButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jToggleButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jToggleButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(tcpButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(udpButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(nullButton)
                .addContainerGap(115, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Agent", jPanel3);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 326, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 239, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("Application", jPanel4);

        jSplitPane1.setLeftComponent(jTabbedPane1);

        getContentPane().add(jSplitPane1, java.awt.BorderLayout.CENTER);

        jToolBar1.setRollover(true);

        jButton1.setText("Run");
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton1);
        jToolBar1.add(jSeparator1);

        configureButton.setText("Configure");
        configureButton.setFocusable(false);
        configureButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        configureButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        configureButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                configureButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(configureButton);

        tcl.setText("Generate Tcl");
        tcl.setFocusable(false);
        tcl.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        tcl.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        tcl.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tclActionPerformed(evt);
            }
        });
        jToolBar1.add(tcl);

        getContentPane().add(jToolBar1, java.awt.BorderLayout.PAGE_START);

        stateLabel.setText("State:");
        getContentPane().add(stateLabel, java.awt.BorderLayout.PAGE_END);

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        StringBuffer sb = new StringBuffer();
        int count = mainPanel.getComponentCount();
        for (int i = 0; i < count; i++) {
            sb.append(mainPanel.getComponent(i).getClass().getName()).append("\n");
            if (mainPanel.getComponent(i).getClass() == ComputerLabel.class) {
            }
        }
        JOptionPane.showMessageDialog(null, sb.toString());
    }//GEN-LAST:event_jButton1ActionPerformed

    private void configureButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_configureButtonActionPerformed
        // TODO add your handling code here:
        Configuration configure = new Configuration(this, true);
        configure.setVisible(true);
        tclName = configure.getTclName().getText();
        traceFileName = configure.getTraceName().getText();
        namName = configure.getNamName().getText();
    }//GEN-LAST:event_configureButtonActionPerformed

    private void tclActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tclActionPerformed
        // TODO add your handling code here:
        StringBuilder tclSb = new StringBuilder();
        tclSb.append("set ns [new Simulator]\nset qsize 20\n");
        File tclFile = new File(tclName + ".tcl");
        try {
            tclSb.append("set ta [open " + traceFileName + ".dat w]\nset tq [open " + traceQueueName + ".dat w]\n$ns trace-all $ta\n"
                    + "set nf [open " + namName + ".nam w]\n$ns namtrace-all $nf\n\nproc finish {} {\n\tglobal ns  ta tq  nf\n\t$ns flush-trace\n\t"
                    + "close $nf\n\texec nam " + namName + ".nam &\n\tclose $ta\n\tclose $tq\n\texit 0\n}\n");
            int count = mainPanel.getComponentCount();
            //read the top and generate the tcl
            for (int i = count - 1; i >= 0; i--) {
                if (NodeLabel.class.isAssignableFrom(mainPanel.getComponent(i).getClass())) {
                    //tclSb.append("set n").append(((NodeLabel) mainPanel.getComponent(i)).getID()).append(" [$ns node]\n");
                } else if (ExtendLineSegmentComponent.class.isAssignableFrom(mainPanel.getComponent(i).getClass())) {
                    ExtendLineSegmentComponent link = (ExtendLineSegmentComponent) mainPanel.getComponent(i);

                    if (link.getLinkType() == LinkType.simplexLink) {
                        //simplex-link tcl
                        NodeLabel first = (NodeLabel) link.getFirst();
                        NodeLabel last = (NodeLabel) link.getLast();
                        if (!first.isTcled()) {
                            tclSb.append("set n").append(first.getID()).append(" [$ns node]\n");
                            first.setTcled(true);
                        }
                        if (!last.isTcled()) {
                            tclSb.append("set n").append(last.getID()).append(" [$ns node]\n");
                            last.setTcled(true);
                        }

                        tclSb.append("$ns simlex-link ");
                        tclSb.append("$n").append(((NodeLabel) link.getFirst()).getID()).append(" $n").append(((NodeLabel) link.getLast()).getID()).append("\n\n");
                    } else if (link.getLinkType() == LinkType.duplexLink) {
                        //duplex-link tcl
                        NodeLabel first = (NodeLabel) link.getFirst();
                        NodeLabel last = (NodeLabel) link.getLast();
                        if (!first.isTcled()) {
                            tclSb.append("set n").append(first.getID()).append(" [$ns node]\n");
                            first.setTcled(true);
                        }
                        if (!last.isTcled()) {
                            tclSb.append("set n").append(last.getID()).append(" [$ns node]\n");
                            last.setTcled(true);
                        }

                        tclSb.append("$ns duplex-link ");
                        tclSb.append("$n").append(((NodeLabel) link.getFirst()).getID()).append(" $n").append(((NodeLabel) link.getLast()).getID()).append("\n\n");
                    } else if (link.getLinkType() == LinkType.commonLink) {
                        //common-link tcl
                        NodeLabel first = (NodeLabel) link.getFirst();
                        NodeLabel last = (NodeLabel) link.getLast();
                        String agentType = null;
                        String nodeType = null;
                        //if the first node is tcled
                        if (!first.isTcled()) {
                            if (AgentLabel.class.isAssignableFrom(first.getClass())) {
                                AgentLabel agent = (AgentLabel) first;
                                if (agent.getClass() == TCPLabel.class) {
                                    tclSb.append("set tcp").append(agent.getID()).append(" [new Agent/TCP]\n");
                                    agentType = "tcp" + agent.getID();
                                } else if (agent.getClass() == UDPLabel.class) {
                                    tclSb.append("set udp").append(agent.getID()).append(" [new Agent/UDP]\n");
                                    agentType = "udp" + agent.getID();
                                } else {
                                    tclSb.append("set null").append(agent.getID()).append(" [new Agent/NULL]\n");
                                    agentType = "null" + agent.getID();
                                }
                                first.setTcled(true);
                            } else {
                                tclSb.append("set n").append(first.getID()).append(" [$ns node]\n");
                                nodeType = "n" + first.getID();
                                first.setTcled(true);
                            }
                            //if the first node isn't tcled
                        } else {
                            if (AgentLabel.class.isAssignableFrom(first.getClass())) {
                                AgentLabel agent = (AgentLabel) first;
                                if (agent.getClass() == TCPLabel.class) {
                                    agentType = "tcp" + agent.getID();
                                } else if (agent.getClass() == UDPLabel.class) {
                                    agentType = "udp" + agent.getID();
                                } else {
                                    agentType = "null" + agent.getID();
                                }
                            } else {
                                nodeType = "n" + first.getID();
                            }
                        }
                        //if the last node is tcled
                        if (!last.isTcled()) {
                            if (AgentLabel.class.isAssignableFrom(last.getClass())) {
                                AgentLabel agent = (AgentLabel) last;
                                if (agent.getClass() == TCPLabel.class) {
                                    tclSb.append("set tcp").append(agent.getID()).append(" [new Agent/TCP]\n");
                                    agentType = "tcp" + agent.getID();
                                } else if (agent.getClass() == UDPLabel.class) {
                                    tclSb.append("set udp").append(agent.getID()).append(" [new Agent/UDP]\n");
                                    agentType = "udp" + agent.getID();
                                } else {
                                    tclSb.append("set null").append(agent.getID()).append(" [new Agent/NULL]\n");
                                    agentType = "null" + agent.getID();
                                }
                                last.setTcled(true);
                            } else {
                                tclSb.append("set n").append(last.getID()).append(" [$ns node]\n");
                                nodeType = "n" + last.getID();
                                last.setTcled(true);
                            }
                        } else {
                            //if first node isn't tcled
                            if (AgentLabel.class.isAssignableFrom(last.getClass())) {
                                AgentLabel agent = (AgentLabel) last;
                                if (agent.getClass() == TCPLabel.class) {
                                    agentType = "tcp" + agent.getID();
                                } else if (agent.getClass() == UDPLabel.class) {
                                    agentType = "udp" + agent.getID();
                                } else {
                                    agentType = "null" + agent.getID();
                                }
                            } else {
                                nodeType = "n" + last.getID();
                            }
                        }
                        tclSb.append("$ns attach-agent $" + nodeType + " $" + agentType + "\n\n");

                    }
                    if (link.getBandWidth() != 0) {
                        tclSb.append(link.getBandWidth()).append("Mb ");
                    }
                    if (link.getDelay() != 0) {
                        tclSb.append(link.getDelay()).append("ms ");
                    }
                    if (link.getQueueType() != null) {
                        tclSb.append(link.getQueueType()).append("\n");
                    } else {
                        tclSb.append("\n");
                    }
                } else if (AgentLabel.class.isAssignableFrom(mainPanel.getComponent(i).getClass())) {
                }
            }
            //temp code
            tclSb.append("$ns queue-limit $n0 $n1 $qsize\n$ns trace-queue $n0 $n1 $tq\n"
                    + "set qmon [$ns monitor-queue $n0 $n1 0.1]\nset fqsize [open \"qs.dat\" w]\n\n"
                    + "proc record {} {\n\tglobal ns qmon fqsize\n\tset time  [$ns now]\n\t"
                    + "set qsize [$qmon set pkts_]\n\tputs $fqsize \"$time $qsize\"\n\t$qmon reset\n\n"
                    + "$ns at [expr $time+1.0] \"record\"\n}\n\n $ns at 0 \"record\"\n$ns at 50.0 \"finish\"\n\n"
                    + "$ns run");
            //#temp code
            BufferedWriter writer = new BufferedWriter(new FileWriter(tclFile));
            writer.write(tclSb.toString());
            writer.close();
        } catch (IOException ex) {
            System.out.println(ex);
        }
        this.stateLabel.setText(tclName + ".tcl has been generated successfully.");
    }//GEN-LAST:event_tclActionPerformed

    private void duplexLineButtonItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_duplexLineButtonItemStateChanged
        // TODO add your handling code here:
        switch (evt.getStateChange()) {
            case ItemEvent.SELECTED:
                mainPanel.setDrawElement(new DrawLine(chooseButton, LinkType.duplexLink));
                break;
            case ItemEvent.DESELECTED:
                mainPanel.setDrawElement(null);
                break;
            default:
                throw new UnsupportedOperationException("Not support the state change code");
        }
}//GEN-LAST:event_duplexLineButtonItemStateChanged

    private void simplexLineButtonItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_simplexLineButtonItemStateChanged
        switch (evt.getStateChange()) {
            case ItemEvent.SELECTED:
                mainPanel.setDrawElement(new DrawLine(chooseButton, LinkType.simplexLink));
                break;
            case ItemEvent.DESELECTED:
                mainPanel.setDrawElement(null);
                break;
            default:
                throw new UnsupportedOperationException("Not support the state change code");
        }
}//GEN-LAST:event_simplexLineButtonItemStateChanged

    private void switchButtonItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_switchButtonItemStateChanged
        // TODO add your handling code here:
        switch (evt.getStateChange()) {
            case ItemEvent.SELECTED:
                mainPanel.setDrawElement(new DrawNode(chooseButton, NodeType.SWITCH));
                break;
            case ItemEvent.DESELECTED:
                mainPanel.setDrawElement(null);
                break;
            default:
                throw new UnsupportedOperationException("Not support the state change code");
        }
}//GEN-LAST:event_switchButtonItemStateChanged

    private void routButtonItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_routButtonItemStateChanged
        switch (evt.getStateChange()) {
            case ItemEvent.SELECTED:
                mainPanel.setDrawElement(new DrawNode(chooseButton, NodeType.ROUT));
                break;
            case ItemEvent.DESELECTED:
                mainPanel.setDrawElement(null);
                break;
            default:
                throw new UnsupportedOperationException("Not support the state change code");
        }
}//GEN-LAST:event_routButtonItemStateChanged

    private void computerButtonItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_computerButtonItemStateChanged
        switch (evt.getStateChange()) {
            case ItemEvent.SELECTED:
                mainPanel.setDrawElement(new DrawNode(chooseButton, NodeType.COMPUTER));
                break;
            case ItemEvent.DESELECTED:
                mainPanel.setDrawElement(null);
                break;
            default:
                throw new UnsupportedOperationException("Not support the state change code");
        }
}//GEN-LAST:event_computerButtonItemStateChanged

    private void tcpButtonItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_tcpButtonItemStateChanged
        // TODO add your handling code here:
        switch (evt.getStateChange()) {
            case ItemEvent.SELECTED:
                mainPanel.setDrawElement(new DrawAgent(chooseButton, AgentType.TCP));
                break;
            case ItemEvent.DESELECTED:
                mainPanel.setDrawElement(null);
                break;
            default:
                throw new UnsupportedOperationException("Not support the state change code");
        }
    }//GEN-LAST:event_tcpButtonItemStateChanged

    private void udpButtonItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_udpButtonItemStateChanged
        // TODO add your handling code here:
        switch (evt.getStateChange()) {
            case ItemEvent.SELECTED:
                mainPanel.setDrawElement(new DrawAgent(chooseButton, AgentType.UDP));
                break;
            case ItemEvent.DESELECTED:
                mainPanel.setDrawElement(null);
                break;
            default:
                throw new UnsupportedOperationException("Not support the state change code");
        }
    }//GEN-LAST:event_udpButtonItemStateChanged

    private void nullButtonItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_nullButtonItemStateChanged
        // TODO add your handling code here:
        switch (evt.getStateChange()) {
            case ItemEvent.SELECTED:
                mainPanel.setDrawElement(new DrawAgent(chooseButton, AgentType.NULL));
                break;
            case ItemEvent.DESELECTED:
                mainPanel.setDrawElement(null);
                break;
            default:
                throw new UnsupportedOperationException("Not support the state change code");
        }
    }//GEN-LAST:event_nullButtonItemStateChanged

    private void lineButtonItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_lineButtonItemStateChanged
        // TODO add your handling code here:
        switch (evt.getStateChange()) {
            case ItemEvent.SELECTED:
                mainPanel.setDrawElement(new DrawLine(chooseButton, LinkType.commonLink));
                break;
            case ItemEvent.DESELECTED:
                mainPanel.setDrawElement(null);
                break;
            default:
                throw new UnsupportedOperationException("Not support the state change code");
        }
    }//GEN-LAST:event_lineButtonItemStateChanged

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JToggleButton chooseButton;
    private javax.swing.JToggleButton computerButton;
    private javax.swing.JButton configureButton;
    private javax.swing.JToggleButton duplexLineButton;
    private javax.swing.JButton jButton1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JSplitPane jSplitPane2;
    private javax.swing.JSplitPane jSplitPane3;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JToggleButton jToggleButton3;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JToggleButton lineButton;
    private edu.qmul.ns2.MainPanel mainPanel;
    private javax.swing.JToggleButton nullButton;
    private javax.swing.JToggleButton routButton;
    private javax.swing.JToggleButton simplexLineButton;
    private javax.swing.JLabel stateLabel;
    private javax.swing.JToggleButton switchButton;
    private javax.swing.JButton tcl;
    private javax.swing.JToggleButton tcpButton;
    private javax.swing.JToggleButton udpButton;
    // End of variables declaration//GEN-END:variables

    public JLabel getStateLabel() {
        return this.stateLabel;
    }

    public Component getInfoComponent() {
        return this.jScrollPane1.getViewport().getView();
    }

    public void setInfoComponent(Component comp) {
        if (comp == null) {
            comp = new JLabel("NULL");
        }
        jScrollPane1.getViewport().setView(comp);
    }
}
